<?php

/**
 * @file CTTMTGDAO.inc.php
 *
 * Copyright (c) 2003-2012 John Willinsky
 * Edit by Ngọc Diệp 2014
 */

import ( 'classes.acct.CTTMTG' );
class CTTMTGDAO extends DAO {
	function &getCTKT_view($query, $paramArray, $rangeInfo) {
		$result =& $this->retrieveRange($query, $paramArray, $rangeInfo);
		$returner = new DAOResultFactory ( $result, $this, '_returnCTKT_view_FromRow' );
		return $returner;
	}

	function &getCTKT_viewNT($query, $paramArray, $rangeInfo) {
		$result =& $this->retrieveRange($query, $paramArray, $rangeInfo);
		$returner = new DAOResultFactory ( $result, $this, '_returnCTKT_viewNT_FromRow' );
		return $returner;

	}

	function &zgetCTKT_view($query, $paramArray) {
		//echo $query;
		$result = & $this->retrieve ( $query, $paramArray );

		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnCTKT_view_FromRow' );
		
		$sections = $returner;
		$i = 0;
		while ( ($section = & $sectionsIterator->next ()) ) {
			$sections [$i] ['idphieu'] = $section->getidphieu ();
			$sections [$i] ['ngct'] = $section->getngct ();
			$sections [$i] ['soct'] = $section->getsoct ();
			$sections [$i] ['diengiai'] = $section->getdiengiai ();
			//$sections [$i] ['noco'] = $section->getnoco ();
			$sections [$i] ['tkdu'] = $section->gettkdu ();
			$sections [$i] ['macttkdu'] = $section->getmacttkdu ();
			$sections [$i] ['tk'] = $section->gettk();
			$sections [$i] ['sotienvn'] = $section->getsotienvn ();
			$i += 1;
			unset ( $section );
		}
		return $sections;
	}


	function &_returnCTKT_view_FromRow(&$row) {
		$CTKT = new CTTMTG();
		$CTKT ->setidphieu($row['idphieu']);
		$CTKT->setsoct($row['soct']);
		$CTKT->setngct($row['ngct']);
		$CTKT->setdiengiai($row['diengiai']);
		$CTKT->settk($row['tk']);
		//$CTKT->setnoco($row['noco']);
		$CTKT->settkdu($row['tkdu']);
		$CTKT->setmacttkdu($row['macttkdu']);
		$CTKT->setsotienvn($row['sotienvn']);
		return $CTKT;
	}


	function &zgetCTKT_viewNT($query, $paramArray) {
		//echo $query;
		$result = & $this->retrieve ( $query, $paramArray );

		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnCTKT_viewNT_FromRow' );
		
		$sections = $returner;
		$i = 0;
		while ( ($section = & $sectionsIterator->next ()) ) {
			$sections [$i] ['idphieu'] = $section->getidphieu ();
			$sections [$i] ['ngct'] = $section->getngct ();
			$sections [$i] ['soct'] = $section->getsoct ();
			$sections [$i] ['diengiai'] = $section->getdiengiai ();
			$sections [$i] ['tkdu'] = $section->gettkdu ();
			$sections [$i] ['macttkdu'] = $section->getmacttkdu ();
			$sections [$i] ['tk'] = $section->gettk();
			$sections [$i] ['sotienvn'] = $section->getsotienvn ();
			$sections [$i] ['sotiennt'] = $section->getsotiennt ();
			$i += 1;
			unset ( $section );
		}
		return $sections;
	}

	function &_returnCTKT_viewNT_FromRow(&$row) {
		$CTKT = new CTTMTG();
		$CTKT ->setidphieu($row['idphieu']);
		$CTKT->setsoct($row['soct']);
		$CTKT->setngct($row['ngct']);
		$CTKT->setdiengiai($row['diengiai']);
		$CTKT->settk($row['tk']);
		$CTKT->settkdu($row['tkdu']);
		$CTKT->setmacttkdu($row['macttkdu']);
		$CTKT->setsotienvn($row['sotienvn']);
		$CTKT->setsotiennt($row['sotiennt']);
		return $CTKT;
	}

//-------nhat----
	function &getCTKT_edit($query) {
		//NewV
		$result = & $this->retrieve( $query );
		$i = 0;
		$sections  = array();
		while (!$result->EOF) {
			$row = $result->GetRowAssoc(false);
			$sections [$i] = $row;
			$i++;
			$result->moveNext();
		}
		return $sections ;
		
		
		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnCTKT_edit_FromRow' );
		
		$sections = $returner;
		$i = 0;
		while ( ($section = & $sectionsIterator->next ()) ) {
			$sections [$i] ['idphieu'] = $section->getidphieu ();
            $sections [$i] ['ngct'] = $section->getngct ();
			$sections [$i] ['soct'] = $section->getsoct ();
			$sections [$i] ['diengiai'] = $section->getdiengiai ();
            //-----------nhat---------------
			$sections [$i] ['daidien'] = $section->getdaidien ();
            $sections [$i] ['mact'] = $section->getmact ();
            $sections [$i] ['ngaylapct'] = $section->getngaylapct ();
            $sections [$i] ['noidungthue'] = $section->getnoidungthue ();
            $sections [$i] ['macttk'] = $section->getmacttk ();
            $sections [$i] ['ma_dk'] = $section->getma_dk ();
            $sections [$i] ['ma_dvcs'] = $section->getma_dvcs ();
            $sections [$i] ['soctcn'] = $section->getsoctcn ();
            $sections [$i] ['ngayctcn'] = $section->getngayctcn ();
            $sections [$i] ['macp'] = $section->getmacp ();
            $sections [$i] ['loaihinh'] = $section->getloaihinh ();
            $sections [$i] ['kihieumd'] = $section->getkihieumd ();
            $sections [$i] ['sohd'] = $section->getsohd ();
            $sections [$i] ['soseri'] = $section->getsoseri ();
            $sections [$i] ['ngayhd'] = $section->getngayhd ();
            $sections [$i] ['makh'] = $section->getmakh ();
			$sections [$i] ['makhthue'] = $section->getmakhthue ();
            $sections [$i] ['masot'] = $section->getmasot ();
            $sections [$i] ['tenkh'] = $section->gettenkh ();
            $sections [$i] ['tentk'] = $section->gettentk ();
            $sections [$i] ['tendv'] = $section->gettendv ();
            $sections [$i] ['mathue'] = $section->getmathue ();
            $sections [$i] ['thuesuat'] = $section->getthuesuat ();
            $sections [$i] ['sotien_doanhso'] = $section->getsotien_doanhso();
            $sections [$i] ['thue'] = $section->getthue ();
            $sections [$i] ['tongtien'] = $section->gettongtien ();
            $sections [$i] ['mavuviec'] = $section->getmavuviec ();
            //-----------end---------------
			$sections [$i] ['diachi'] = $section->getdiachi ();
			$sections [$i] ['lydo'] = $section->getlydo ();
			$sections [$i] ['solgctg'] = $section->getsolgctg ();
			$sections [$i] ['mant'] = $section->getmant ();
			$sections [$i] ['tygia'] = $section->gettygia ();
			$sections [$i] ['noco'] = $section->getnoco ();
			$sections [$i] ['tkdu'] = $section->gettkdu ();
			$sections [$i] ['macttkdu'] = $section->getmacttkdu ();
			$sections [$i] ['macttkthue'] = $section->getmacttkthue ();
			$sections [$i] ['tk'] = $section->gettk();
			$sections [$i] ['sotienvn'] = $section->getsotienvn ();
			$sections [$i] ['sotiennt'] = $section->getsotiennt ();
            $sections [$i] ['tenhh'] = $section->gettenhh ();
			$i += 1;
			unset ( $section );
		}
		return $sections;
	}

	function &_returnCTKT_edit_FromRow(&$row) {
		$CTKT = new CTTMTG();
		$CTKT ->setidphieu($row['idphieu']);
		$CTKT->setsoct($row['soct']);
		$CTKT->setngct($row['ngct']);
		$CTKT->setdiachi($row['diachi']);
		$CTKT->setlydo($row['lydo']);
		$CTKT->setsolgctg($row['solgctg']);
		$CTKT->setmant($row['mant']);
		$CTKT->settygia($row['tygia']);
		$CTKT->settk($row['tk']);
		$CTKT->settkdu($row['tkdu']);
		$CTKT->setmacttkdu($row['macttkdu']);
		$CTKT->setmacttkthue($row['macttkthue']);
		$CTKT->setsotienvn($row['sotienvn']);
		$CTKT->setsotiennt($row['sotiennt']);
		$CTKT->setdiengiai($row['diengiai']);
        //-----------nhat---------------
        $CTKT->setdaidien($row['daidien']);
        $CTKT->setmact($row['mact']);
        $CTKT->setngaylapct($row['ngaylapct']);
        $CTKT->setnoidungthue($row['noidungthue']);
        $CTKT->setmacttk($row['macttk']);
        $CTKT->setma_dk($row['ma_dk']);
        $CTKT->setma_dvcs($row['ma_dvcs']);
        $CTKT->setsoctcn($row['soctcn']);
        $CTKT->setngayctcn($row['ngayctcn']);
        $CTKT->setmacp($row['macp']);
        $CTKT->setloaihinh($row['loaihinh']);
        $CTKT->setkihieumd($row['kihieumd']);
        $CTKT->setsohd($row['sohd']);
        $CTKT->setsoseri($row['soseri']);
        $CTKT->setngayhd($row['ngayhd']);
        $CTKT->setmakh($row['makh']);
        $CTKT->settentk($row['tentk']);
        $CTKT->settendv($row['tendv']);
		$CTKT->setmakhthue($row['makhthue']);
        $CTKT->setmasot($row['masot']);
        $CTKT->settenkh($row['tenkh']);
        $CTKT->settenhh($row['tenhh']);
        $CTKT->setmathue($row['mathue']);
        $CTKT->setthuesuat($row['thuesuat']);
        $CTKT->setsotien_doanhso($row['sotien_doanhso']);
        $CTKT->setthue($row['thue']);
        $CTKT->settongtien($row['tongtien']);
        $CTKT->setmavuviec($row['mavuviec']);
        //-----------end ---------------
		return $CTKT;

	}


function &getCTKT_BCTMTG_v2($journalid, $nam, $ngay1, $ngay2, $tk, $mant) {
		$sections = $returner;
		$query = "SELECT dknovn, dknont FROM SDTT" .$journalid. $nam ;
		$query = $query . " WHERE MATK like '" . $tk . "%' && MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		while (!$result->EOF) {
			$rows = $result->GetRowAssoc(false);
			$sections [0] ['sodu']  = floatval($row['dknovn']);
			$sections [0] ['sodunt']  = floatval($row['dknont']);
			$sections [0] ['psco'] = "";
			$sections [0] ['psno'] = "";

			$sections [0] ['pscont'] = "";
			$sections [0] ['psnont'] = "";
			$result->moveNext();

		}
		$sections [0] ['diengiai']  = "Số dư đầu kỳ:";
		
		$query =  "SELECT sum( b.sotiennt), sum(b.sotienvn) FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'N'	&& MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		$daukyno=$result->GetRowAssoc(false);
		$daukynont =floatval($daukyno["sum(b.sotiennt)"]);
		$daukynovn =floatval($daukyno["sum(b.sotienvn)"]);
		
		$sections [0] ['sodu']  += $daukynovn ;
		$sections [0] ['sodunt']  += $daukynovn ;

		$query =  "SELECT sum( b.sotiennt), sum(b.sotienvn) FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'C'	and MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		$daukyco=$result->GetRowAssoc(false);
		$daukycont = floatval($daukyco["sum(b.sotiennt)"]);
		$daukycovn = floatval($daukyco["sum(b.sotienvn)"]);
		$sections [0] ['sodu']  -=$daukycovn ;
		$sections [0] ['sodunt']  -=$daukycont ;
		if ($sections [0] ['sodu']   == 0)
			$sections [0] ['sodu']   ="";

		if ($sections [0] ['sodunt']   == 0)
			$sections [0] ['sodunt']   ="";

		$query = "SELECT a.soct, a.ngct, b.diengiai, a.mant, b.tygia, a.TK , a.macttk , b.tkdu , b.macttkdu, b.sotiennt, b.sotienvn, b.macttkdu AS madt, a.mact, a.noco FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt" .$journalid. $nam . " b ON (a.idphieu = b.idphieu) ";
		
		$query = $query . "WHERE DATE( a.ngct ) >= '" . $ngay1 . "' && DATE( a.ngct ) <=  '" . $ngay2 . "' && a.TK like '" . $tk . "%'and MANT like '".$mant. "%'";
		$query = $query . "		ORDER BY DATE_FORMAT(ngct, '%Y %m %d'), soct";

		$result = & $this->retrieve ( $query );


		$i = 1;
		$spsno = 0;
		$spsco = 0;

		if ($mant == "VND")
		{
			while (!$result->EOF) {
				$rows = $result->GetRowAssoc(false);
				$sections [$i] ['ngct'] = $rows['ngct'];;
				$sections [$i] ['soct'] = $rows['soct'];;
				$sections [$i] ['diengiai'] = $rows['diengiai'];;
				$sections [$i] ['tkdu'] = $rows['tkdu'];;
				$sections [$i] ['tk'] = $rows['tk'];;
				$sections [$i] ['noco'] = $rows['noco'];;
				if ($sections [$i] ['noco'] =='N')
				{
					$sections [$i] ['psno'] = floatval($rows['sotienvn']);
					$sections [$i] ['psco'] = "";
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] + floatval($rows['psno']) ;
					$spsno  += floatval($sections [$i] ['psno']) ;

		
				}
				else
				{
					$sections [$i] ['psno'] = "";
					$sections [$i] ['psco'] = floatval($rows['sotienvn']);
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] - floatval($sections [$i] ['psco']) ;
					$spsco  += floatval($sections [$i] ['psco']) ;
				}

				$i += 1;
				unset ( $section );
				$result->moveNext();
			}
			$sections [$i] ['ngct'] = $sections [$i-1] ['ngct'];
			$sections [$i] ['soct'] = "";
			if ($spsno >0)
				$sections [$i] ['psno'] = $spsno;
			else
				$sections [$i] ['psno'] = "";
			if ($spsco >0)
				$sections [$i] ['psco'] = $spsco;
			else
				$sections [$i] ['psco'] = "";

			$sections [$i] ['sodu']  = $sections [$i-1] ['sodu']  ;
			$sections [$i] ['diengiai'] ="Tổng phát sinh";
		}
		else
		{
			$spsnont = 0;
			$spscont = 0;
			while (!$result->EOF) {
				$rows = $result->GetRowAssoc(false);
				$sections [$i] ['ngct'] = $rows['ngct'];;
				$sections [$i] ['soct'] = $rows['soct'];;
				$sections [$i] ['diengiai'] = $rows['diengiai'];;
				$sections [$i] ['tkdu'] = $rows['tkdu'];;
				$sections [$i] ['tk'] = $rows['tk'];;
				$sections [$i] ['noco'] = $rows['noco'];;
				if ($sections [$i] ['noco'] =='N')
				{
					$sections [$i] ['psno'] = floatval($rows['sotienvn']);
					$sections [$i] ['psnont'] = floatval($rows['sotiennt']);
					$sections [$i] ['psco'] = "";
					$sections [$i] ['pscont'] = "";
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] + floatval($rows['psno']) ;
					$sections [$i] ['sodunt'] = $sections [$i-1] ['sodunt'] + floatval($rows['psnont']) ;
					$spsno  += floatval($sections [$i] ['psno']) ;
					$spsnont  += floatval($sections [$i] ['psnont']) ;


		
				}
				else
				{
					$sections [$i] ['psno'] = "";
					$sections [$i] ['psnont'] = "";
					$sections [$i] ['psco'] = floatval($rows['sotienvn']);
					$sections [$i] ['pscont'] = floatval($rows['sotiennt']);
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] - floatval($sections [$i] ['psco']) ;
					$sections [$i] ['sodunt'] = $sections [$i-1] ['sodunt'] - floatval($sections [$i] ['pscont']) ;
					$spsco  += floatval($sections [$i] ['psco']) ;
					$spscont  += floatval($sections [$i] ['pscont']) ;
				}

				$i += 1;
				unset ( $section );
				$result->moveNext();
			}
		
			$sections [$i] ['ngct'] = $sections [$i-1] ['ngct'];
			$sections [$i] ['soct'] = "";
			if ($spsno >0)
				$sections [$i] ['psno'] = $spsno;
			else
				$sections [$i] ['psno'] = "";
			if ($spsco >0)
				$sections [$i] ['psco'] = $spsco;
			else
				$sections [$i] ['psco'] = "";

			if ($spsnont >0)
				$sections [$i] ['psnont'] = $spsnont;
			else
				$sections [$i] ['psnont'] = "";
			if ($spscont >0)
				$sections [$i] ['pscont'] = $spscont;
			else
				$sections [$i] ['pscont'] = "";


			$sections [$i] ['sodu']  = $sections [$i-1] ['sodu']  ;
			$sections [$i] ['sodunt']  = $sections [$i-1] ['sodunt']  ;
			$sections [$i] ['diengiai'] ="Số dư cuối kỳ";

		}
		return $sections;
	}	



function &getCTKT_BCTMTG($journalid, $nam, $ngay1, $ngay2, $tk, $mant) {
		$query = "SELECT a.soct, a.ngct, b.diengiai, a.mant, b.sotien_tygia, a.TK , a.macttk , b.tkdu , b.macttkdu, b.sotiennt, b.sotienvn, a.mact, a.noco, b.id FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt" .$journalid. $nam . " b ON (a.idphieu = b.idphieu) ";
		
		$query = $query . "WHERE DATE( a.ngct ) >= '" . $ngay1 . "' && DATE( a.ngct ) <=  '" . $ngay2 . "' && a.TK like '" . $tk . "%'and MANT like '".$mant. "%'";
		//$query = $query . "		ORDER BY ngct, if(noco='N', 1,2), soct";

		$query = $query . " UNION SELECT a.soct, a.ngct, b.diengiai, a.mant, b.sotien_tygia, b.tkdu as tk , b.macttkdu as macttk, a.tk as tkdu , a.macttk as macttkdu, b.sotiennt, b.sotienvn, a.mact, if(a.noco='N', 'C', 'N') as noco,  b.id FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt" .$journalid. $nam . " b ON (a.idphieu = b.idphieu) ";
		
		$query = $query . "WHERE DATE( a.ngct ) >= '" . $ngay1 . "' && DATE( a.ngct ) <=  '" . $ngay2 . "' && b.TKdu like '" . $tk . "%'and MANT like '".$mant. "%'";

		$query = $query . "		ORDER BY ngct, if(noco='N', 1,2), soct";
		$result = & $this->retrieve ( $query );

		$returner = new DAOResultFactory ( $result, $this, '_returnCTKT_BCTMTG_FromRow' );
		return $returner;
	}

function &getCTKT_BCTMTG_PSDK($journalid, $nam, $ngay1, $ngay2, $tk, $mant) {

		$query =   "SELECT sum( if(noco = 'N', b.sotiennt, 0)) as psnont, sum(if(noco = 'N',  b.sotienvn,0)) as psnovn, sum( if(noco = 'C', b.sotiennt, 0)) as pscont, sum(if(noco = 'C',  b.sotienvn,0)) as pscovn FROM CTKT".$journalid. $nam."  a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu)  WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' and MANT like '".$mant. "%'";

		$result = & $this->retrieve ( $query );
		$result=$result->GetRowAssoc(false);
		return $result;
}

function &getCTKT_BCTMTG_SDDK($journalid, $nam, $ngay1, $ngay2, $tk, $mant) {
		$sections = $returner;
		$query = "SELECT sum(dknovn) as dknovn, sum(dknont) as dknont FROM SDTT" .$journalid. $nam ;
		$query = $query . " WHERE MATK like '" . $tk . "%' && MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		$result=$result->GetRowAssoc(false);
		return $result;

}


function &zzgetCTKT_BCTMTG($journalid, $nam, $ngay1, $ngay2, $tk, $mant) {
		$sections = $returner;
		$query = "SELECT dknovn, dknont FROM SDTT" .$journalid. $nam ;
		$query = $query . " WHERE MATK like '" . $tk . "%' && MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnSDTT_FromRow' );
		$sections [0] ['diengiai']  = "Số dư đầu kỳ:";
		while ( ($section = & $sectionsIterator->next ()) ) {
			$sections [0] ['sodu']  = floatval($section->getdknovn());
			$sections [0] ['sodunt']  = floatval($section->getdknont());
			$sections [0] ['psco'] = "";
			$sections [0] ['psno'] = "";

			$sections [0] ['pscont'] = "";
			$sections [0] ['psnont'] = "";

		}
		$sections [0] ['diengiai']  = "Số dư đầu kỳ:";

		// tinh cac ps dau ky:
		$query =  "SELECT sum( b.sotiennt) as psnont, sum(b.sotienvn) as psnovn FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'N'	&& MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		$daukyno=$result->GetRowAssoc(false);
		if ($daukyno["psnont"] !== NULL)
			$daukynont =floatval($daukyno["psnont"]);
		else
			$daukynont =0;

		if ($daukyno["psnovn"] !== NULL)
			$daukynovn =floatval($daukyno["psnovn"]);
		else
			$daukynovn =0;

		$sections [0] ['sodu']  += $daukynovn ;
		$sections [0] ['sodunt']  += $daukynovn ;

		$query =  "SELECT sum( b.sotiennt) as pscont, sum(b.sotienvn) as pscovn FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'C'	and MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		$daukyco=$result->GetRowAssoc(false);

		if ($daukyco["pscont"] !== NULL)
			$daukycont =floatval($daukyco["pscont"]);
		else
			$daukycont =0;

		if ($daukyco["pscovn"] !== NULL)
			$daukycovn =floatval($daukyco["pscovn"]);
		else
			$daukycovn =0;


		$sections [0] ['sodu']  -=$daukycovn ;
		$sections [0] ['sodunt']  -=$daukycont ;

		if ($sections [0] ['sodu']   == 0)
			$sections [0] ['sodu']   ="";

		if ($sections [0] ['sodunt']   == 0)
			$sections [0] ['sodunt']   ="";


		$query = "SELECT a.soct, a.ngct, DATE_FORMAT(a.ngct, '%Y %m %d') as ngayct,  b.diengiai, a.mant, b.sotien_tygia, a.TK , a.macttk , b.tkdu , b.macttkdu, b.sotiennt, b.sotienvn, b.macttkdu AS madt, a.mact, a.noco FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt" .$journalid. $nam . " b ON (a.idphieu = b.idphieu) ";
		
		$query = $query . "WHERE DATE( a.ngct ) >= '" . $ngay1 . "' && DATE( a.ngct ) <=  '" . $ngay2 . "' && a.TK like '" . $tk . "%'and MANT like '".$mant. "%'";
		$query = $query . "		ORDER BY ngayct, soct";
		$result = & $this->retrieve ( $query );


		$i = 1;
		$spsno = 0;
		$spsco = 0;
		//$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnCTKT_BCTMTG_FromRow' );

		$returner = new DAOResultFactory ( $result, $this, '_returnCTKT_BCTMTG_FromRow' );

		return $returner;

		

		if ($mant == "VND")
		{
			while ( ($section = & $sectionsIterator->next ()) ) {
				$sections [$i] ['ngct'] = $section->getngct ();
				$sections [$i] ['ngayct'] = $section->getngayct ();
				$sections [$i] ['soct'] = $section->getsoct ();
				$sections [$i] ['diengiai'] = $section->getdiengiai ();
				$sections [$i] ['tkdu'] = $section->gettkdu ();
				$sections [$i] ['tk'] = $section->gettk ();
				$sections [$i] ['noco'] = $section->getnoco ();
				if ($sections [$i] ['noco'] =='N')
				{
					$sections [$i] ['psno'] = floatval($section->getsotienvn ());
					$sections [$i] ['psco'] = "";
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] + floatval($section->getsotienvn ()) ;
					$spsno  += floatval($sections [$i] ['psno']) ;

				}
				else
				{
					$sections [$i] ['psno'] = "";
					$sections [$i] ['psco'] = floatval($section->getsotienvn ());
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] - floatval($sections [$i] ['psco']) ;
					$spsco  += floatval($sections [$i] ['psco']) ;
				}

				$i += 1;
				unset ( $section );
			}
			$sections [$i] ['ngct'] = date("Y m d");
			$sections [$i] ['ngayct'] = date("Y m d")+1;

			$sections [$i] ['soct'] = "zzzzzz";
			if ($spsno >0)
				$sections [$i] ['psno'] = $spsno;
			else
				$sections [$i] ['psno'] = "";
			if ($spsco >0)
				$sections [$i] ['psco'] = $spsco;
			else
				$sections [$i] ['psco'] = "";

			$sections [$i] ['sodu']  = $sections [$i-1] ['sodu']  ;
			$sections [$i] ['diengiai'] ="Số dư cuối kỳ";
		}
		else
		{
			$spsnont = 0;
			$spscont = 0;
			while ( ($section = & $sectionsIterator->next ()) ) {
				$sections [$i] ['ngct'] = $section->getngct ();
				$sections [$i] ['ngayct'] = $section->getngayct ();
				$sections [$i] ['soct'] = $section->getsoct ();
				$sections [$i] ['diengiai'] = $section->getdiengiai ();
				$sections [$i] ['tkdu'] = $section->gettkdu ();
				$sections [$i] ['tk'] = $section->gettk ();
				$sections [$i] ['noco'] = $section->getnoco ();
				if ($sections [$i] ['noco'] =='N')
				{
					$sections [$i] ['psno'] = floatval($section->getsotienvn ());
					$sections [$i] ['psnont'] = floatval($section->getsotiennt ());
					$sections [$i] ['psco'] = "";
					$sections [$i] ['pscont'] = "";
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] + floatval($section->getsotienvn ()) ;
					$sections [$i] ['sodunt'] = $sections [$i-1] ['sodunt'] + floatval($section->getsotiennt ()) ;
					$spsno  += floatval($sections [$i] ['psno']) ;
					$spsnont  += floatval($sections [$i] ['psnont']) ;


		
				}
				else
				{
					$sections [$i] ['psno'] = "";
					$sections [$i] ['psnont'] = "";
					$sections [$i] ['psco'] = floatval($section->getsotienvn ());
					$sections [$i] ['pscont'] = floatval($section->getsotiennt ());
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] - floatval($sections [$i] ['psco']) ;
					$sections [$i] ['sodunt'] = $sections [$i-1] ['sodunt'] - floatval($sections [$i] ['pscont']) ;
					$spsco  += floatval($sections [$i] ['psco']) ;
					$spscont  += floatval($sections [$i] ['pscont']) ;
				}

				$i += 1;
				unset ( $section );
			}
		
			$sections [$i] ['ngct'] = date("Y m d");
			$sections [$i] ['ngayct'] = date("Y m d")+1;
			if ($spsno >0)
				$sections [$i] ['psno'] = $spsno;
			else
				$sections [$i] ['psno'] = "";
			if ($spsco >0)
				$sections [$i] ['psco'] = $spsco;
			else
				$sections [$i] ['psco'] = "";

			if ($spsnont >0)
				$sections [$i] ['psnont'] = $spsnont;
			else
				$sections [$i] ['psnont'] = "";
			if ($spscont >0)
				$sections [$i] ['pscont'] = $spscont;
			else
				$sections [$i] ['pscont'] = "";


			$sections [$i] ['sodu']  = $sections [$i-1] ['sodu']  ;
			$sections [$i] ['sodunt']  = $sections [$i-1] ['sodunt']  ;
			$sections [$i] ['diengiai'] ="Số dư cuối kỳ";

		}
		return $sections;
	}	

	function &zgetCTKT_BCTMTG($journalid, $nam, $ngay1, $ngay2, $tk) {
		$sections = $returner;
		

		$query = "SELECT dknovn FROM SDTT" .$journalid. $nam ;
		$query = $query . " WHERE MATK like '" . $tk . "%'";
		$result = & $this->retrieve ( $query );
		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnSDTT_FromRow' );
		$sections [0] ['diengiai']  = "Số dư đầu kỳ:";
		while ( ($section = & $sectionsIterator->next ()) ) {
			$sections [0] ['sodu']  = intval($section->getdknovn());
			$sections [0] ['psco'] = "";
			$sections [0] ['psno'] = "";

		}
		// tinh so dau ky
		$query =  "SELECT sum( b.sotiennt), sum(b.sotienvn) FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'N'	";
		$result = & $this->retrieve ( $query );
		$daukyno=$result->GetRowAssoc(false);
		$daukynont =floatval($daukyno["sum(b.sotiennt)"]);
		$daukynovn =floatval($daukyno["sum(b.sotienvn)"]);
		
		$sections [0] ['sodu']  += $daukynovn ;

		$query =  "SELECT sum( b.sotiennt), sum(b.sotienvn) FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'C'	";
		$result = & $this->retrieve ( $query );
		$daukyco=$result->GetRowAssoc(false);
		$daukycont = floatval($daukyco["sum(b.sotiennt)"]);
		$daukycovn = floatval($daukyco["sum(b.sotienvn)"]);
		$sections [0] ['sodu']  -=$daukycovn ;

		$query = "SELECT a.soct, a.ngct, b.diengiai, a.mant, b.sotien_tygia, a.TK , a.macttk , b.tkdu , b.macttkdu, b.sotiennt, b.sotienvn, b.macttkdu AS madt, a.mact, a.noco FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt" .$journalid. $nam . " b ON (a.idphieu = b.idphieu) ";
		
		$query = $query . "WHERE DATE( a.ngct ) >= '" . $ngay1 . "' && DATE( a.ngct ) <=  '" . $ngay2 . "' && a.TK like '" . $tk . "%'";
		$query = $query . "		ORDER BY ngct, soct";

		$result = & $this->retrieve ( $query );
		echo $query;
		return;

		
		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnCTKT_BCTMTG_FromRow' );

		

		$i = 1;
		$spsno = 0;
		$spsco = 0;
		while ( ($section = & $sectionsIterator->next ()) ) {
			
			$sections [$i] ['ngct'] = $section->getngct ();
			$sections [$i] ['soct'] = $section->getsoct ();
			$sections [$i] ['diengiai'] = $section->getdiengiai ();
			$sections [$i] ['tkdu'] = $section->gettkdu ();
			$sections [$i] ['tk'] = $section->gettk();
			$sections [$i] ['noco'] = $section->getnoco ();
			if ($sections [$i] ['noco'] =='N')
			{
				$sections [$i] ['psno'] = $section->getsotienvn ();
				$sections [$i] ['psco'] = "";
				$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] + intval($sections [$i] ['psno']) ;
				$spsno  += intval($sections [$i] ['psno']) ;

	
			}
			else
			{
				$sections [$i] ['psno'] = "";
				$sections [$i] ['psco'] = $section->getsotienvn ();
				$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] - intval($sections [$i] ['psco']) ;
				$spsco  += intval($sections [$i] ['psco']) ;
			}

			$i += 1;
			unset ( $section );
		}
			$sections [$i] ['ngct'] = $sections [$i-1] ['ngct'];
			$sections [$i] ['soct'] = "";
			//$sections [$i] ['tkdu'] = "";
			//$sections [$i] ['tk'] = "";
			//$sections [$i] ['noco'] = "N";
			$sections [$i] ['psno'] = $spsno;
			$sections [$i] ['psco'] = $spsco;
			

			$sections [$i] ['sodu']  = $sections [$i-1] ['sodu']  ;
			$sections [$i] ['diengiai'] ="Số dư cuối kỳ";

		return $sections;
	}



	function &_returnCTKT_BCTMTG_FromRow(&$row) {
		$CTKT = new CTTMTG();
		$CTKT ->setidphieu($row['idphieu']);
		$CTKT->setsoct($row['soct']);
		$CTKT->setngct($row['ngct']);
		$CTKT->setngayct($row['ngayct']);
		$CTKT->setdaidien($row['daidien']);
		$CTKT->setdiachi($row['diachi']);
		$CTKT->setlydo($row['lydo']);
		$CTKT->setsolgctg($row['solgctg']);
		$CTKT->setmant($row['mant']);
		$CTKT->settygia($row['tygia']);
		$CTKT->settk($row['tk']);
		$CTKT->settkdu($row['tkdu']);
        $CTKT->setma_dvcs($row['ma_dvcs']);
		$CTKT->setmacttkdu($row['macttkdu']);
		$CTKT->setsotienvn($row['sotienvn']);
		$CTKT->setsotiennt($row['sotiennt']);
		$CTKT->setdiengiai($row['diengiai']);
		$CTKT->setnoco($row['noco']);
		//HookRegistry::call('CTKTDAO::_returnCTKT_BCTMTG_FromRow', array(&$CTKT, &$row));
		return $CTKT;

	}


	function &getCTKT_ThuChi($query) {
		$result = & $this->retrieve ( $query );
		$sections = $returner ;
		$i = 0;
		while (!$result->EOF) {
			$rows = $result->GetRowAssoc(false);
			$pmatk = $rows['matk'];
			$ptentk = $rows['tentk'];
			$sections[$pmatk."-".$ptentk] = $pmatk."-".$ptentk;

			if ($i==0)
			{
				$sections [0] ['ngct'] = $rows['ngct'] ;
				$sections [0] ['soct'] = $rows['soct'] ;
				$sections [0] ['daidien'] = $rows['daidien'] ;
				$sections [0] ['diachi'] = $rows['diachi'] ;
				$sections [0] ['lydo'] = $rows['lydo'] ;
				$sections [0] ['solgctg'] = $rows['solgctg'] ;
				$sections [0] ['tkdu'] = $rows['tkdu'] ;
				$sections [0] ['tk'] = $rows['tk'] ;
				$sections [0] ['sotienvn'] = floatval($rows['sotienvn']) ;
			}
			else
			{
				$sections [$i] ['tkdu'] = $rows['tkdu'];
				$cotkdu = 0;
				for ($x=0; $x <$i; $x++) {
					if ($sections [$i] ['tkdu'] != $sections [$x] ['tkdu'])
					{
						$cotkdu = 1;
					}
										
				} 
				if ($cotkdu == 1)
					$sections [0] ['tkdu'] .= "-" .  $rows['tkdu'];

				$sections [0] ['sotienvn'] += floatval($rows['sotienvn']) ;


			}
			$i += 1;

			unset($section);
			$result->moveNext();
		}
 	   return $sections;
	}

	function &_returnCTKT_ThuChi_FromRow(&$row) {
		$CTKT = new CTTMTG();
		$CTKT->setsoct($row['soct']);
		$CTKT->setngct($row['ngct']);
		$CTKT->setdaidien($row['daidien']);
		$CTKT->setdiachi($row['diachi']);
		$CTKT->setlydo($row['lydo']);
		$CTKT->setsolgctg($row['solgctg']);
		$CTKT->settk($row['tk']);
		$CTKT->settkdu($row['tkdu']);
		$CTKT->setsotienvn($row['sotienvn']);
		return $CTKT;

	}

	function &_returnSDTT_FromRow(&$row) {
		$CTKT = new CTTMTG();
		$CTKT->setdknovn($row['dknovn']);
		$CTKT->setdknont($row['dknont']);
				return $CTKT;

	}

	function CreateTable($Year, $journalid ) {
		$sql = "CREATE TABLE CTTMTG".$journalid.$Year." LIKE str_CTTMTG" ;
		$result =& $this->retrieve($sql );
		$sql = "CREATE TABLE CTTMTG".$journalid.$Year."CT LIKE str_CTTMTGCT" ;
		$result =& $this->retrieve($sql );

		$sql = "CREATE TABLE CTTMTG".$journalid.$Year."THUE LIKE str_CTTMTGTHUE" ;
		$result =& $this->retrieve($sql );
		return;
	}
}

?>
